// 通用工具函数

// 显示消息
function showMessage(element, text, type) {
    if (!element) {
        console.error('消息元素未找到');
        return;
    }
    
    element.textContent = text;
    element.className = 'message ' + type;
    element.classList.add('show');
    
    // 自动隐藏成功消息
    if (type === 'success') {
        setTimeout(() => {
            hideMessage(element);
        }, 5000);
    }
}

// 隐藏消息
function hideMessage(element) {
    if (element) {
        element.classList.remove('show');
    }
}

// 设置按钮加载状态
function setButtonLoading(button, isLoading, loadingText = null) {
    if (!button) return;
    
    const originalHTML = button.innerHTML;
    const originalText = button.textContent;
    
    if (isLoading) {
        button.disabled = true;
        button.innerHTML = '<span class="loading"></span>' + (loadingText || originalText);
    } else {
        button.disabled = false;
        button.innerHTML = originalHTML.replace(/<span class="loading"><\/span>/, '');
    }
}

// 验证函数
function validateEmail(email) {
    const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return re.test(email);
}

function validatePhone(phone) {
    const re = /^1[3-9]\d{9}$/;
    return re.test(phone.replace(/\s+/g, ''));
}

function formatPhoneNumber(phone) {
    const cleaned = phone.replace(/\D/g, '');
    const match = cleaned.match(/^(\d{3})(\d{4})(\d{4})$/);
    if (match) {
        return match[1] + ' ' + match[2] + ' ' + match[3];
    }
    return phone;
}

// 倒计时函数
function startCountdown(button, seconds = 60) {
    let timeLeft = seconds;
    const originalText = button.innerHTML;
    
    const timer = setInterval(() => {
        button.innerHTML = `重新发送(${timeLeft}s)`;
        button.disabled = true;
        timeLeft--;
        
        if (timeLeft < 0) {
            clearInterval(timer);
            button.innerHTML = originalText;
            button.disabled = false;
        }
    }, 1000);
    
    return timer;
}

// 显示用户信息
function showUserInfoCommon(user, userEmailElement, userDisplayNameElement, avatarElement) {
    if (userEmailElement) {
        userEmailElement.textContent = user.email || '用户';
    }
    if (userDisplayNameElement) {
        userDisplayNameElement.textContent = user.email ? user.email.split('@')[0] : '用户';
    }
    if (avatarElement) {
        const displayName = user.email || 'U';
        avatarElement.textContent = displayName.charAt(0).toUpperCase();
    }
}

// 检查用户登录状态
async function checkAuth() {
    try {
        const { data: { session } } = await supabase.auth.getSession();
        return session;
    } catch (error) {
        console.error('检查认证状态错误:', error);
        return null;
    }
}

// 密码强度验证
function validatePassword(password) {
    const errors = [];
    
    if (password.length < 6) {
        errors.push('密码长度至少6位');
    }
    if (!/(?=.*[a-z])/.test(password)) {
        errors.push('至少包含一个小写字母');
    }
    if (!/(?=.*[A-Z])/.test(password)) {
        errors.push('至少包含一个大写字母');
    }
    if (!/(?=.*\d)/.test(password)) {
        errors.push('至少包含一个数字');
    }
    
    return {
        valid: errors.length === 0,
        errors: errors
    };
}